package com.iasku.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.iasku.constant.Constant;

public class WebRequest {
    public static String getResultByUrlAjax(String serverurl, String params_string, String methodname) {
        HttpClient httpClient = new DefaultHttpClient();
        String uri = serverurl + "ajax/" + methodname + params_string;
        HttpGet httpGet = new HttpGet(uri);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    String re = EntityUtils.toString(httpEntity);
                    return re;
                }
            } else {
                return null;
            }
            httpClient.getConnectionManager().closeExpiredConnections();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static String getResultByUrl(String serverurl, String params_string, String methodname) {
        HttpClient httpClient = new DefaultHttpClient();
        String uri = serverurl + "api/" + methodname + params_string;
        HttpGet httpGet = new HttpGet(uri);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    String re = EntityUtils.toString(httpEntity);
                    return re;
                }
            } else {
                return null;
            }
            httpClient.getConnectionManager().closeExpiredConnections();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String postResultByUrl(String methodname, Map<String, String> req_params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(Constant.SERVERURL + "api/" + methodname);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        if (req_params != null && !req_params.isEmpty()) {
            for (Entry<String, String> entry : req_params.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        try {
            // HTTP request
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            // HTTP response
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // 200 ok+
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = httpResponse.getEntity();
                return EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }
}
